local priority = 5

EntitySystem.updatesystem("trackPlayerUpdate", priority, function(dt)
    for e in pairs(EntitySystem.query("playerTracker")) do
      if e.physics then
          
        --Get direction of player and rotate towards them
        local a = math.angle(e.position.x, e.position.y, player.position.x, player.position.y)        
        e.physics.body:setAngle(-1*a + e.trackPlayer.turnSpeed)
        
      --Fire at player
      if e.reload then
        local d = math.dist(e.position.x,e.position.y,player.position.x,player.position.y)
        if d < 1500 and e.type.num == 1 then
          e.reload.isFiring = true
          
        else
          e.reload.isFiring = false
        end
      end
      
      
        --Move entity
        if e.movement then
          e.physics.body:setLinearVelocity(math.cos(e.physics.body:getAngle())*e.movement.speed, math.sin(e.physics.body:getAngle())*e.movement.speed)
        end
        
        
        
      end
      
    end
end)